package com.laguille.games.tetris.model.Tetromino;

public abstract class TetrominoPiece {
	
	protected TetrominoBlock[] blocks;
	
	/**
	 * For the piece position
	 *  @see http://tetris.wikia.com/wiki/SRS
	 *  We try to use block[1] as invariant and block[3] - sometimes in combination with block[2] - as pointing 
	 *  to the direction (UP, LEFT, DOWN, RIGHT) when possible.
	 */
	protected Position position;
	
	
	public TetrominoPiece()
	{
		blocks = new TetrominoBlock[4];
	}
	
	public TetrominoPiece(Coordinates...coordinates)
	{
		this();
		blocks[0] = new TetrominoBlock(coordinates[0]);
		blocks[1] = new TetrominoBlock(coordinates[1]);
		blocks[2] = new TetrominoBlock(coordinates[2]);
		blocks[3] = new TetrominoBlock(coordinates[3]);
	}

	public TetrominoBlock[] getBlocks() {
		return blocks;
	}
	
	/**
	 * 
	 * rotates a piece to the left
	 * @return a copy of the piece object rotated on the left
	 */
	public abstract TetrominoPiece rotateLeft();

	/**
	 * 
	 * rotates a piece to the right
	 * @return a copy of the piece object rotated on the right
	 */
	public abstract TetrominoPiece rotateRight();
	
	@Override
	public String toString() {
		StringBuffer sb = new StringBuffer();
		sb.append("Piece ").append(this.getClass().getName()).append(":\n");
		for (int i = 0 ; i < blocks.length ; i++)
		{
			sb.append(String.format("Block %d: %s\n", i, blocks[i].toString()));
		}
		return sb.toString();
	}

	public Position getPosition() {
		return position;
	}

	public void setPosition(Position position) {
		this.position = position;
	}
	
	

}
